project(Demo)
set(CMAKE_CXX_STANDARD 17)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)

if (WIN32)
    set(CMAKE_CXX_FLAGS "-mwindows")
endif()

if(CMAKE_BUILD_TYPE AND (CMAKE_BUILD_TYPE STREQUAL "Debug"))
    add_definitions(-DBUILD_TYPE_DEBUG)
endif()

add_subdirectory(plugin)

# Find the Qt library
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Gui Core Network Svg)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Gui Core Network Svg)

include_directories(${CMAKE_SOURCE_DIR}/utkwidget)
include_directories(${CMAKE_SOURCE_DIR}/utkcore)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/plugin)

#生成ts文件的文件夹
set(TS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/translations")

set(TS_FILES
    "${TS_DIR}/Demo_zh_CN.ts"
    "${TS_DIR}/Demo_en.ts"
)

#模拟更新和发布的命令代码
find_program(LUPDATE_EXECUTABLE lupdate)
find_program(LRELEASE_EXECUTABLE lrelease)
find_program(LCONVERT_EXECUTABLE lconvert)

#生成ts和qm文件
foreach(_ts_file ${TS_FILES})
    execute_process(COMMAND ${LUPDATE_EXECUTABLE} -recursive ${CMAKE_CURRENT_SOURCE_DIR} -ts ${_ts_file})
endforeach()

execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/translations")

FILE(GLOB HEADERS
    ${CMAKE_CURRENT_SOURCE_DIR}/*.h
)

FILE(GLOB SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
)

set(PROJECT_SOURCES
    ${HEADERS}
    ${SOURCES}
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_resources(qrc_FILES ${CMAKE_CURRENT_SOURCE_DIR}/resouces.qrc)
    qt_add_executable(Demo ${PROJECT_SOURCES} ${qrc_FILES})
else()
    qt5_add_resources(qrc_FILES ${CMAKE_CURRENT_SOURCE_DIR}/resouces.qrc)
    add_executable(Demo ${PROJECT_SOURCES} ${qrc_FILES})
endif()

target_link_libraries(Demo PUBLIC
    Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Svg
    Qt${QT_VERSION_MAJOR}::Network utkwidget utkcore
)

set(ALLTS_FILES
    "${CMAKE_SOURCE_DIR}/utkwidget/translations/utkwidget_zh_CN.ts"
    "${CMAKE_SOURCE_DIR}/utkwidget/translations/utkwidget_en.ts"
    "${TS_DIR}/Demo_zh_CN.ts"
    "${TS_DIR}/Demo_en.ts"
)

foreach(ts_file ${ALLTS_FILES})
    execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${ts_file} "${CMAKE_CURRENT_BINARY_DIR}/translations")
endforeach()

execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/libs ${CMAKE_CURRENT_BINARY_DIR})

FILE(GLOB ENTS
    ${CMAKE_CURRENT_BINARY_DIR}/translations/*_en.ts
)
string(JOIN " " entslist ${ENTS})
string(APPEND execonvert ${LCONVERT_EXECUTABLE})
string(APPEND execonvert " -i ${entslist} -o ${CMAKE_CURRENT_BINARY_DIR}/translations/example_en.ts")
message("list ents: ${execonvert}")

if(WIN32)
    execute_process(COMMAND powershell -Command ${execonvert})
else()
    execute_process(COMMAND bash -c ${execonvert})
endif()

FILE(GLOB ZHCNTS
    ${CMAKE_CURRENT_BINARY_DIR}/translations/*_zh_CN.ts
)
string(JOIN " " zhcnlist ${ZHCNTS})
string(APPEND exezhconvert ${LCONVERT_EXECUTABLE})
string(APPEND exezhconvert " -i ${zhcnlist} -o ${CMAKE_CURRENT_BINARY_DIR}/translations/example_zh_CN.ts")
message("list ents: ${exezhconvert}")

if(WIN32)
    execute_process(COMMAND powershell -Command ${exezhconvert})
else()
    execute_process(COMMAND bash -c ${exezhconvert})
endif()

set(EXAMPLE_FILES
    "${CMAKE_CURRENT_BINARY_DIR}/translations/example_en.ts"
    "${CMAKE_CURRENT_BINARY_DIR}/translations/example_zh_CN.ts"
)

foreach(_ts_file ${EXAMPLE_FILES})
    add_custom_command(TARGET Demo PRE_BUILD COMMAND ${LRELEASE_EXECUTABLE} ${_ts_file})
endforeach()
